//
//  PopAnimator.swift
//  weibo
//
//  Created by hm on 2020/4/27.
//  Copyright © 2020 hm. All rights reserved.
//

import UIKit

class PopAnimator: NSObject{
    // MARK:- 属性
    var isPresented = false
    //定义弹出PopView的位置及尺寸
    var presentedFrame: CGRect = CGRect.zero
    //定义弹出状态切换回调
    var mCallBack: ((_ presented: Bool)->())?
    
    //注意：自定义函数会覆盖掉默认init构造函数
    init(callBack: @escaping (_ presented:Bool) -> ()){
        self.mCallBack = callBack
    }
}

// MARK:- 弹出PopView的转场代理
extension PopAnimator: UIViewControllerTransitioningDelegate{
    //自定义弹出PopView的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let pop = PopOverPresentationController(presentedViewController: presented, presenting: presenting)
        pop.presentedView?.frame = presentedFrame
        return pop
    }
    
    //自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
    
    //自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

// MARK:- 弹出/消失动画的代理
extension PopAnimator: UIViewControllerAnimatedTransitioning{
    //定义动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    //获取转场的上下文
    //通过上下文获取弹出/消失的PopView
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented = !isPresented
        //执行回调
        mCallBack?(isPresented)
        //执行弹出/消失动画
        isPresented ? animationForPresented(transitionContext: transitionContext) : animationForDismissed(transitionContext: transitionContext)
    }
    
    //弹出动画
    private func animationForPresented(transitionContext: UIViewControllerContextTransitioning){
        //1)获取弹出的PopView
        let presentedView = transitionContext.view(forKey: .to)!
        //2)将弹出PopView加入containerView
        transitionContext.containerView.addSubview(presentedView)
        //3)执行动画
        //定义起始尺寸
        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        //定义锚点为顶边中心点
        presentedView.layer.anchorPoint = CGPoint(x:0.5,y:0)
        //定义终点尺寸
        UIView.animate(withDuration: transitionDuration(using: transitionContext),
                       //定义目标动画：恢复PopView的原始尺寸
                       animations: {
                            presentedView.transform = CGAffineTransform.identity },
                       //必须告诉上下文已经完成了动画
                       completion: {(_)-> Void in
                            transitionContext.completeTransition(true)
                        })
    }
    
    //消失动画
    private func animationForDismissed(transitionContext: UIViewControllerContextTransitioning){
        let dismissView = transitionContext.view(forKey: .from)
        UIView.animate(withDuration: transitionDuration(using: transitionContext),
                       animations: {
                        //IOS的bug:y的值不能写为0.0
                        dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
                        },
                        completion: {(_)-> Void in
                            dismissView?.removeFromSuperview()
                            transitionContext.completeTransition(true)
                        })
    }
    
    
}
